package com.lzm.ydpt.module.mall.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.collection.SimpleArrayMap;
import androidx.core.content.ContextCompat;

import com.byt.framlib.baseadapter.rv.RvCommonAdapter;
import com.byt.framlib.baseadapter.rv.RvViewHolder;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.mall.ProductSpecsBean;
import com.lzm.ydpt.entity.mall.ShopGoods;
import com.lzm.ydpt.entity.mall.SpecBean;
import com.lzm.ydpt.module.mall.view.SKUViewGroup;

import java.util.List;

/**
 * Created by 胡逸枫 on 2017/1/16.
 */

public class GoodsAttrsAdapter extends RvCommonAdapter<ProductSpecsBean> {

    private ISku myInterface;

    private SimpleArrayMap<Integer, String> saveClick;

    private List<ShopGoods> stockGoodsList;//商品数据集合
    private List<ProductSpecsBean> mList;//商品数据集合
    private SpecBean[] selectedValue;   //选中的属性
    private SpecBean[][] childrenSpes;    //二维 装所有属性
    private TextView[][] childrenViews;    //二维 装所有按钮

    private final int SELECTED = 0x100;
    private final int CANCEL = 0x101;

    public GoodsAttrsAdapter(Context ctx, List<ProductSpecsBean> list, List<ShopGoods> stockGoodsList) {
        super(ctx, list, R.layout.item_skuattrs);
        this.stockGoodsList = stockGoodsList;
        this.mList = list;
        saveClick = new SimpleArrayMap<>();
        childrenViews = new TextView[list.size()][0];
        childrenSpes = new SpecBean[list.size()][0];
        selectedValue = new SpecBean[list.size()];
        for (int i = 0; i < list.size(); i++) {
            selectedValue[i] = null;
        }
    }

    public void setSKUInterface(ISku iSku) {
        this.myInterface = iSku;
    }


    private int focusPositionG, focusPositionC;

    @Override
    protected void convert(RvViewHolder holder, ProductSpecsBean attributesBean, int position) {
        TextView tv_ItemName = holder.getView(R.id.tv_ItemName);
        tv_ItemName.setText(attributesBean.getSpec_name());
        SKUViewGroup vg_skuItem = holder.getView(R.id.vg_skuItem);
        List<SpecBean> childrens = attributesBean.getItems();
        int childrenSize = childrens.size();
        TextView[] textViews = new TextView[childrenSize];
        SpecBean[] specsBeans = new SpecBean[childrenSize];
        for (int i = 0; i < childrenSize; i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(20, 15, 20, 15);
            TextView textView = new TextView(mContext);
            textView.setGravity(Gravity.CENTER);
            textView.setPadding(30, 10, 30, 10);
            textView.setLayoutParams(params);
            textView.setBackgroundResource(R.drawable.shap_product_sku_select);
            textView.setText(childrens.get(i).getSpec_name());
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.color_333333));
            textView.setTextSize(14);
            textViews[i] = textView;
            specsBeans[i] = childrens.get(i);
            vg_skuItem.addView(textViews[i]);
        }
        childrenViews[position] = textViews;
        childrenSpes[position] = specsBeans;
        initOptions();
        canClickOptions();
        getSelected();
    }

    public void setSelect(ShopGoods selectGoods) {
        String[] specIds = selectGoods.getSpec_ids().split(",");
        for (int i = 0, size = mList.size(); i < size; i++) {
            for (SpecBean specBean : mList.get(i).getItems()) {
                if (specIds[i].equals(specBean.getSpec_id() + "")) {
                    selectedValue[i] = specBean;
                }
            }
        }
        initOptions();
        canClickOptions();
        getSelected();
    }

    private class MyOnClickListener implements View.OnClickListener {
        //点击操作 选中SELECTED   取消CANCEL
        private int operation;
        // 属性
        private int positionG;
        // 规格
        private int positionC;

        public MyOnClickListener(int operation, int positionG, int positionC) {
            this.operation = operation;
            this.positionG = positionG;
            this.positionC = positionC;
        }

        @Override
        public void onClick(View v) {
            focusPositionG = positionG;
            focusPositionC = positionC;
            SpecBean apecsBean = childrenSpes[positionG][positionC];
            switch (operation) {
                case SELECTED:
                    saveClick.put(positionG, positionC + "");
                    selectedValue[positionG] = apecsBean;
                    myInterface.selectedAttribute(selectedValue);
                    break;
                case CANCEL:
                    saveClick.put(positionG, "");
                    for (int l = 0; l < selectedValue.length; l++) {
                        if (selectedValue[l] == apecsBean) {
                            selectedValue[l] = null;
                            break;
                        }
                    }
                    myInterface.uncheckAttribute(selectedValue);
                    break;
            }
            initOptions();
            canClickOptions();
            getSelected();
        }
    }

    /**
     * 初始化选项（不可点击，焦点消失）
     */
    private void initOptions() {
        for (int y = 0; y < childrenViews.length; y++) {
            for (int z = 0; z < childrenViews[y].length; z++) {//循环所有属性
                TextView textView = childrenViews[y][z];
                textView.setEnabled(false);
                textView.setTextColor(ContextCompat.getColor(mContext, R.color.color_999999));//变灰
            }
        }
    }

    /**
     * 找到符合条件的选项变为可选
     */
    private void canClickOptions() {
        for (int i = 0, viewlength = childrenViews.length; i < viewlength; i++) { // 第一行的View
            for (int j = 0, listSize = stockGoodsList.size(); j < listSize; j++) {
                boolean filter = false;
                String[] spec_ids = stockGoodsList.get(j).getSpec_ids().split(",");
                for (int k = 0, selectLenth = selectedValue.length; k < selectLenth; k++) {
                    if (i == k || selectedValue[k] == null) {
                        continue;
                    }
                    if (!(selectedValue[k].getSpec_id() + "").equals(spec_ids[k])) {
                        filter = true;
                        break;
                    }
                }
                if (!filter) {
                    for (int n = 0; n < childrenViews[i].length; n++) {
                        TextView textView = childrenViews[i][n];// 拿到一行所有按钮
                        SpecBean setelectBean = childrenSpes[i][n]; // 获取属性
                        //拿到属性名称,id相同而且要有库存
                        if (spec_ids[i].equals(setelectBean.getSpec_id() + "") && stockGoodsList.get(j).getInventory() > 0) {
                            textView.setEnabled(true);//符合就变成可点击
                            //不要让焦点乱跑
                            textView.setTextColor(ContextCompat.getColor(mContext, R.color.color_333333));
                            textView.setOnClickListener(new MyOnClickListener(SELECTED, i, n) {
                            });
                        }
                    }
                }
            }
        }
    }

    /**
     * 找到已经选中的选项，让其变红
     */
    private void getSelected() {
        for (int i = 0; i < childrenViews.length; i++) {
            for (int j = 0; j < childrenViews[i].length; j++) {//拿到每行属性Item
                TextView textView = childrenViews[i][j];//拿到所有属性TextView
                SpecBean apecsBeans = childrenSpes[i][j];
                for (int m = 0; m < selectedValue.length; m++) {
                    if (selectedValue[m] == apecsBeans) {
                        textView.setTextColor(ContextCompat.getColor(mContext, R.color.color_main));
                        textView.setOnClickListener(new MyOnClickListener(CANCEL, i, j));
                    }
                }
            }
        }
    }
}